<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData,
		hasValue
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				dataList:[],
				itemCahceKey: "hr-u-tlApproval-handleInfo-item-cache",
				handleCache:"hr-u-tlApproval-handleInfo-handle-cache",
				userCacheKey:"hr-u-tlApproval-handleInfo-user-cache",
				exists:[]
			})
		},
		methods: {
			verifyHandlePos(){
				let _that = this
				let editor = _that.$refs.autoform.getEditor("handleInfo","itemId")
				if(!_that.$refs.autoform.formdata.itemId){
					_that.om.toast("请先选择"+editor.title,3000)
					return false
				}else{
					return true
				}
			},
			verifyHandleUser(){
				let _that = this
				let editor = _that.$refs.autoform.getEditor("handleInfo","handlePosId")
				if(!_that.$refs.autoform.formdata.handlePosId){
					_that.om.toast("请先选择"+editor.title,3000)
					return false
				}else{
					return true
				}
			},
			getHandleUserList(posId){
				let _that = this
				_that.$refs.autoform.changeEditor("handleInfo","handleUserId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							posId:posId
						}),
						url:"orgInstPosUserOfficial/getUserByPos",
						initload:true,
						namefield:"username",
						cachename:_that.userCacheKey
					}
				})
			},
			getHandlePosList(id){
				let _that = this
				_that.$refs.autoform.changeEditor("handleInfo","handlePosId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							containBoard:true
						}),
						url:"tinstitution/findNotBoard1",
						method:"post",
						namefield:"instName",
						initload:true,
						cachename:_that.handleCache
					}
				})
			}
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.itemCahceKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.itemId = d.id
							formdata.itemName = d.itemName
							_that.getHandlePosList(d.id)
							uni.removeStorage({
								key: _that.itemCahceKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.handleCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.handlePosId = d.id
							formdata.handlePosName = d.instName
							_that.$refs.autoform.clear("handleInfo","handleUserId")
							_that.getHandleUserList(d.id)
							uni.removeStorage({
								key:_that.handleCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.handleUserId = d.id
							formdata.handleUserName = d.username
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				if (e.configDataCacheKey) {
					_that.exists = _that.datas.filter((e,i)=>{
						return (i != _that.index)
					}).map(e=>e.instId).join(",")
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							if (res.data && Object.prototype.toString.call(res.data) === '[object Array]') {
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								changeEditor("handleInfo", "itemId", {
									link: "../../public/pageSearch/pageSearch",
									"linkParam": {
										other: JSON.stringify({
											x: 2,
											itemIds:_that.exists
										}),
										method:"post",
										namefield: "itemName",
										notefield: "remark",
										url: 'tlApproval/getItemCombobox',
										initload: true,
										cachename: _that.itemCahceKey
									}
								},null,_that.autoformdata)
							}
						}
					})
				}
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.handlePosId){
						_that.getHandleUserList(_that.$refs.autoform.formdata.handlePosId)
					}
					if(_that.$refs.autoform.formdata.itemId){
						_that.getHandlePosList(_that.$refs.autoform.formdata.itemId)
					}
					
				})
			})
		}
	}
</script>

<style>
</style>
